package com.bryantkobe.myspringcode.exception.massert;

import com.bryantkobe.myspringcode.exception.ExceptionEnum;
import com.bryantkobe.myspringcode.factory.BusinessExceptionFactory;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @description: 业务断言工具类
 * @author: alienwarezzl
 * @date: 2025/9/23
 */
@Component
public class BusinessAssert {
    private final BusinessExceptionFactory exceptionFactory;

    public BusinessAssert(BusinessExceptionFactory exceptionFactory) {
        this.exceptionFactory = exceptionFactory;
    }

    /**
     * 基础断言方法 - 使用Spring的Assert工具但抛出BusinessException
     */
    public void isTrue(boolean expression, ExceptionEnum exceptionEnum, Object... args) {
        if (!expression) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    public void notNull(Object object, ExceptionEnum exceptionEnum, Object... args) {
        if (object == null) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    public void notEmpty(Collection<?> collection, ExceptionEnum exceptionEnum, Object... args) {
        if (collection == null || collection.isEmpty()) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    public void hasText(String text, ExceptionEnum exceptionEnum, Object... args) {
        if (text == null || text.trim().isEmpty()) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    /**
     * 状态检查断言
     */
    public void state(boolean state, ExceptionEnum exceptionEnum, Object... args) {
        if (!state) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }
}
